/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * pesquisaFuncionario.java
 *
 * Created on 02/06/2010, 14:31:40
 */
package visao;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import modelo.dao.PesquisaDAO;

/**
 *
 * @author Rodo Hotel
 */
public class pesquisaFuncionario extends javax.swing.JFrame {

    private String nome;
    private String cpf;

    public pesquisaFuncionario() {
        initComponents();

        setLocationRelativeTo(null);
        setResizable(false);

        jtConsulta.addMouseListener(
                new MouseAdapter() {

                    @Override
                    public void mouseClicked(
                            MouseEvent e) {

                        // Se clicado 2 vezes ...

                        if (e.getClickCount() == 2) {
                            // retorna a linah selecioanda da tabela
                            int linha = jtConsulta.getSelectedRow();
                            //pega o valor da celula linha referente a o id
                            int id = Integer.valueOf(jtConsulta.getValueAt(linha, 0).toString());

                            //usa o construtor q recebe o id do funcionario
                            dadosFuncionario dadosFunc = new dadosFuncionario(id, nome);
                            // inicializa a tela
                            dadosFunc.setVisible(true);
                            setVisible(false);

                        }
                    }
                });
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jcbPesquisar = new javax.swing.JComboBox();
        jtf = new javax.swing.JTextField();
        jbPesquisar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtConsulta = new javax.swing.JTable();
        jbVerDadosCompleto = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jcbPesquisar.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Selecione...", "Nome", "Cpf" }));

        jbPesquisar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images_icons/016.png"))); // NOI18N
        jbPesquisar.setToolTipText("Pesquisar");
        jbPesquisar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbPesquisarActionPerformed(evt);
            }
        });

        jtConsulta.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null}
            },
            new String [] {
                "Código", "Nome", "CTP", "Telefone Celular", "Telefone Residencial", "Data Admissão", "Data Demissão"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jtConsulta);

        jbVerDadosCompleto.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images_icons/054.png"))); // NOI18N
        jbVerDadosCompleto.setText("Ver dados completo");
        jbVerDadosCompleto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbVerDadosCompletoActionPerformed(evt);
            }
        });

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images_icons/100.png"))); // NOI18N
        jButton1.setToolTipText("Sair");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jcbPesquisar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jtf, javax.swing.GroupLayout.PREFERRED_SIZE, 325, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jbPesquisar))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 916, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jbVerDadosCompleto)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton1)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jbPesquisar)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jcbPesquisar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jtf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(27, 27, 27)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 184, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jButton1)
                    .addComponent(jbVerDadosCompleto))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbPesquisarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbPesquisarActionPerformed

        pesquisar();
}//GEN-LAST:event_jbPesquisarActionPerformed

    private void jbVerDadosCompletoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbVerDadosCompletoActionPerformed
        // TODO add your handling code here:

        int linha = jtConsulta.getSelectedRow();
        //pega o valor da celula linha referente a o id
        int id = Integer.valueOf(jtConsulta.getValueAt(linha, 0).toString());

        //usa o construtor que recebe o id do funcionario
        dadosFuncionario dadosFunc = new dadosFuncionario(id, nome);

        // inicializa a tela
        dadosFunc.setVisible(true);
        setVisible(false);
}//GEN-LAST:event_jbVerDadosCompletoActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        dispose();
}//GEN-LAST:event_jButton1ActionPerformed
    public void preencherTabela(String sql) {
        PesquisaDAO pDAO = new PesquisaDAO();
        DefaultTableModel modelo = (DefaultTableModel) jtConsulta.getModel();
        modelo.setNumRows(0);

        jtConsulta.getColumnModel().getColumn(0).setPreferredWidth(5);
        jtConsulta.getColumnModel().getColumn(1).setPreferredWidth(200);
        jtConsulta.getColumnModel().getColumn(2).setPreferredWidth(30);
        jtConsulta.getColumnModel().getColumn(3).setPreferredWidth(50);
        jtConsulta.getColumnModel().getColumn(4).setPreferredWidth(50);
        jtConsulta.getColumnModel().getColumn(5).setPreferredWidth(25);
        jtConsulta.getColumnModel().getColumn(6).setPreferredWidth(25);

        pDAO.pesquisaPreencherTabela(sql);

        try {
            while (pDAO.rs.next()) {
                modelo.addRow(new Object[]{
                            pDAO.rs.getString("id"),
                            pDAO.rs.getString("nome"),
                            pDAO.rs.getString("ctp"),
                            pDAO.rs.getString("telcel"),
                            pDAO.rs.getString("telresid"),
                            pDAO.rs.getString("dtadmissao"),
                            pDAO.rs.getString("dtdemissao")});
            }
        } catch (SQLException erro) {
            JOptionPane.showMessageDialog(null, "Erro ao listar na Tabela " + erro);
        } catch (ArrayIndexOutOfBoundsException erro) {
            JOptionPane.showMessageDialog(null, "Erro ao listar na Tabela " + erro);
        }
    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new pesquisaFuncionario().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbPesquisar;
    private javax.swing.JButton jbVerDadosCompleto;
    private javax.swing.JComboBox jcbPesquisar;
    private javax.swing.JTable jtConsulta;
    private javax.swing.JTextField jtf;
    // End of variables declaration//GEN-END:variables

    public void pesquisar(String nome) {

        String sql = "select * from funcionario where nome like '" + nome + "%'";

        preencherTabela(sql);

    }

    public void pesquisar() {

        if (jcbPesquisar.getSelectedItem() == "Nome") {
            nome = jtf.getText().trim().toUpperCase();
            String sql = "select * from funcionario where nome like '" + nome + "%' "
                    + "ORDER BY nome";

            preencherTabela(sql);

        } else {

            if (jcbPesquisar.getSelectedItem() == "Cpf") {

                cpf = jtf.getText().trim();
                String sql = "select * from funcionario where cpf like '" + cpf + "%'"
                        + "ORDER BY cpf";;

                preencherTabela(sql);

            }
        }
    }
}
